﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using RookieLibSharp.Progress;
using RookieLibSharp.Configuration;
namespace BirthdayRemainder
{
    public  class ListViewOperate
    {

        public ListView LstView { get; set; }
        public int SortColumn{get;set;}
        private const int THD_ID = 0;
        public ListViewOperate(ListView LstView)
        {
            this.LstView = LstView;
            var type = LstView.GetType();
            var info = type.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            info.SetValue(this.LstView, true, null);
        }
        public int InitLstView()
        {
            UpdateData(true);
            Clear();
            List<PersonInfo> LstInfos = SftMgr.DbGetAllInfo();
            ProgressManager.Init(THD_ID, "初始化列表", LstInfos.Count);
            for (int idx = 0; idx < LstInfos.Count; idx++)
            {
                ListViewItem item = Convert2ListViewItem(LstInfos[idx]);
                item.Text = (idx + 1).ToString();
                AddItem(item);
                ProgressManager.UpdateVale(THD_ID);
            }
            UpdateData(false);
            int nRet = GetItemsCount();
            Sort();
            ProgressManager.CloseProcessFrm();
            return nRet;
        }
        private delegate void dg_AddItem(ListViewItem item);
        /// <summary>
        /// 列表_添加项
        /// </summary>
        /// <param name="LstViewItem"></param>
        public void AddItem(ListViewItem LstViewItem)
        {
            if (LstView.InvokeRequired)
            {
                dg_AddItem method = AddItem;
                LstView.Invoke(method, LstViewItem);
            }
            else
            {
                int itemCnt = GetItemsCount();
                LstViewItem.Text = (itemCnt).ToString();
                this.LstView.Items.Add(LstViewItem);
                Sort();
            }
        }
        private delegate ListViewItem dg_GetItem(int idx);
        /// <summary>
        /// 列表_获取项
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        public ListViewItem GetItem(int idx)
        {
            if (LstView.InvokeRequired)
            {
                dg_GetItem method = GetItem;
                return (ListViewItem)LstView.Invoke(method, idx);
            }
            else
            {
                if (idx < GetItemsCount())
                {
                    return LstView.Items[idx];
                }
                else
                {
                    return null;
                }
            }
        }
        private delegate ListViewItem dg_GetSelectedItem(int idx);
        /// <summary>
        /// 列表_获取选中的项
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        public ListViewItem GetSelectedItem(int idx)
        {
            if (LstView.InvokeRequired)
            {
                dg_GetItem method = GetSelectedItem;
                return (ListViewItem)LstView.Invoke(method, idx);
            }
            else
            {
                if (idx < GetSelectedItemsCount())
                {
                    return this.LstView.SelectedItems[idx];
                }
                else
                {
                    return null;
                }
            }
        }

        private delegate void dg_RemoveAt(int idx);
        /// <summary>
        /// 列表_移除项
        /// </summary>
        /// <param name="idx"></param>
        public void RemoveAt(int idx)
        {
            if (this.LstView.InvokeRequired)
            {
                dg_RemoveAt method = RemoveAt;
                LstView.Invoke(method, idx);
            }
            else
            {
                LstView.Items.RemoveAt(idx);
            }
        }
        private delegate void dg_Clear();
        /// <summary>
        /// 列表_清空内容
        /// </summary>
        public void Clear()
        {
            if (this.LstView.InvokeRequired)
            {
                dg_Clear method = Clear;
                LstView.Invoke(method);
            }
            else
            {
                this.LstView.Items.Clear();
            }
        }
        private delegate void dg_UpdateData(bool BeginOrEnd);
        /// <summary>
        /// 列表_开始结束更新
        /// </summary>
        /// <param name="BeginOrEnd"></param>
        public void UpdateData(bool BeginOrEnd)
        {
            if (this.LstView.InvokeRequired)
            {
                dg_UpdateData method = UpdateData;
                LstView.Invoke(method, BeginOrEnd);
            }
            else
            {
                if (BeginOrEnd)
                {
                    this.LstView.BeginUpdate();
                }
                else
                {
                    this.LstView.EndUpdate();
                }

            }
        }
        private delegate int dg_GetItemsCount();
        /// <summary>
        /// 列表_获取所有项数量
        /// </summary>
        /// <returns></returns>
        public int GetItemsCount()
        {
            if (this.LstView.InvokeRequired)
            {
                dg_GetItemsCount method = GetItemsCount;
                return (int)LstView.Invoke(method);
            }
            else
            {
                return this.LstView.Items.Count;
            }
        }
        private delegate int dg_GetCheckedItemsCount();
        /// <summary>
        /// 列表_获取勾选项数量
        /// </summary>
        /// <returns></returns>
        public int GetCheckedItemsCount()
        {
            if (this.LstView.InvokeRequired)
            {
                dg_GetCheckedItemsCount method = GetCheckedItemsCount;
                return (int)LstView.Invoke(method);
            }
            else
            {
                return this.LstView.CheckedItems.Count;
            }
        }
        private delegate ListViewItem dg_GetCheckedItem(int idx);
        /// <summary>
        /// 列表_获取勾选的项
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        public ListViewItem GetCheckedItem(int idx)
        {
            if (this.LstView.InvokeRequired)
            {
                dg_GetItem method = GetCheckedItem;
                return (ListViewItem)LstView.Invoke(method, idx);
            }
            else
            {
                if (idx < GetCheckedItemsCount())
                {
                    return this.LstView.CheckedItems[idx];
                }
                else
                {
                    return null;
                }
            }
        }


        private delegate int dg_GetSelectedItemsCount();
        /// <summary>
        /// 列表_获取选中项数量
        /// </summary>
        /// <returns></returns>
        public int GetSelectedItemsCount()
        {
            if (this.LstView.InvokeRequired)
            {
                dg_GetSelectedItemsCount method = GetSelectedItemsCount;
                return (int)LstView.Invoke(method);
            }
            else
            {
                return this.LstView.SelectedItems.Count;
            }
        }

        private delegate void dg_SetBackColor(int idx);
        /// <summary>
        /// 列表_设置背景颜色
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="color"></param>
        public void SetBackColor()
        {
            int nDays;
            bool bDisBackColor;
            for (int idx = 0; idx < GetItemsCount(); idx++)
            {
                ListViewItem item = GetItem(idx);
                try
                {
                    nDays = Convert.ToInt32(item.SubItems[SortColumn].Text);
                    bDisBackColor = nDays + (int)DateTime.Today.DayOfWeek < 7;
                }
                catch
                {
                    bDisBackColor = false;
                }
                SetLstViewItemBackColor(item, bDisBackColor);
            }
        }
        private delegate void dg_Refresh();
        /// <summary>
        /// 列表_刷新
        /// </summary>
        public void Refresh()
        {
            if (this.LstView.InvokeRequired)
            {
                dg_Refresh method = Refresh;
                LstView.Invoke(method);
            }
            else
            {
                LstView.Refresh();
            }
        }
        public int DeleteSelectedItems()
        {
            UpdateData(true);
            for (int idx = GetCheckedItemsCount() - 1; idx >= 0; idx--)
            {
                ListViewItem item = GetCheckedItem(idx);
                SftMgr.DbDeleteName(item.SubItems[1].Text);
                RemoveAt(item.Index);
            }
            int nRet = GetItemsCount();
            UpdateData(false);
            Refresh();
            Sort();
            return nRet;
        }

        /// <summary>
        /// 转换为列表项
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ListViewItem Convert2ListViewItem(PersonInfo info)
        {
            ListViewItem item = new ListViewItem();
            item.Text = "";
            item.SubItems.Add(info.Name);
            item.SubItems.Add(info.Birthday);
            item.SubItems.Add(info.SolorOrLunar);
            item.SubItems.Add(info.DayToBirthday.ToString());
            item.SubItems.Add(info.Phone);
            SetLstViewItemBackColor(item, info.DisplayBackColor);
            return item;
        }
        /// <summary>
        /// 转换成列表项
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ListViewItem Convert2ListViewItem(StPersonInfo info)
        {
            ListViewItem item = new ListViewItem();
            item.Text = "";
            item.SubItems.Add(info.Name);
            item.SubItems.Add(info.Birthday);
            item.SubItems.Add(info.SolorOrLunar);
            item.SubItems.Add(info.DayToBirthday.ToString());
            item.SubItems.Add(info.Phone);
            SetLstViewItemBackColor(item, info.DisplayBackColor);
            return item;
        }
        private delegate void dg_Sort();
        public void Sort()
        {
            if (LstView.InvokeRequired)
            {
                dg_Sort method = Sort;
                LstView.Invoke(method);
            }
            else
            {
                ListViewCompare compare = new ListViewCompare(SortColumn);
                LstView.ListViewItemSorter = compare;
                string serial;
                for (int idx = 0; idx < GetItemsCount(); idx++)
                {
                    serial = (idx + 1).ToString();
                    ListViewItem item = GetItem(idx);
                    if (item.SubItems[0].Text != serial)
                    {
                        item.SubItems[0].Text = serial;
                    }
                }
            }
        }
        private Color GetBackColor()
        {
            int intVal = Cfg.ReadInt(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_LST_VIEW_BACK_COLOR);
            Color BackColor = Color.FromArgb(intVal == 0 ? GlobalValue.SFT_CFG_LST_VIEW_BACK_COLOR_DEFAULT : intVal);
            return BackColor;
        }
      
        public void SetLstViewItemBackColor(ListViewItem item,bool bSet)
        {
            if (bSet)
            {
                item.BackColor = GetBackColor();
            }
            else
            {
                item.BackColor = Color.FromKnownColor(KnownColor.Window);
            }
        }
    }
}
